<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/";
%>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <link rel="icon" href="../../static/resources/favicon.ico">
    <link rel="stylesheet" href="../../static/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../static/resources/css/public.css" media="all"/>
</head>
<body style="margin:10px;">
<!-- 搜索条件开始 -->
<div align="center">
    <form class="layui-form" method="post" id="searchForm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">供应商:</label>
                <div class="layui-input-inline">
                    <select name="providerId" lay-verify="required" autocomplete="off"
                            class="layui-input" id="providerId1">
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称:</label>
                <div class="layui-input-inline">
                    <select name="goodsId" lay-verify="required" autocomplete="off"
                            class="layui-input" id="goodsId1">
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间:</label>
                <div class="layui-input-inline">
                    <input type="text" name="begintime" id="begintime" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间:</label>
                <div class="layui-input-inline">
                    <input type="text" name="endtime" id="endtime" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-inline" style="margin-left:50px;margin-top:-8px;margin-bottom: -3px">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-radius layui-icon layui-icon-search"
                        lay-submit lay-filter="doSearch" id="doSearch">查询
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius layui-icon layui-icon-refresh">
                    重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 搜索条件结束 -->


<!-- 数据表格开始 -->
<table class="layui-hide" id="inportTable" lay-filter="inportTable" lay-data="{id: 'inportTable'}"></table>
<!--表格工具条-->
<%--<div style="display: none;" id="tableToolBar">--%>
<%--    <button type="button" class="layui-btn layui-btn-sm" lay-event="add">--%>
<%--        <i class="layui-icon">&#xe608;</i> 进货--%>
<%--    </button>--%>
<%--</div>--%>
<!-- 行级工具条 -->
<div id="rowToolBar" style="display: none;">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="export">退货</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding:20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">供应商:</label>
            <div class="layui-input-inline">
                <input id="providerid2" class="layui-input" autocomplete="off" type='text' disabled='disabled' lay-verify="required" name="providerName" />
                <input type="hidden" name="providerId">
            </div>
            <label class="layui-form-label">商品名称:</label>
            <div class="layui-input-inline">
                    <input id="goodsid2" lay-verify="required" autocomplete="off" name="goodsName" class="layui-input" type="text" disabled="disabled">
                <input type="hidden" name="goodsId">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label">进货数量:</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsNum" class="layui-input" placeholder="请输入进货数量" disabled="disabled">
                <input type="hidden" name="goodsNum">
            </div>
            <label class="layui-form-label">进货价格:</label>
            <div class="layui-input-inline">
                <input type="text" name="pur_price" placeholder="请输入进货价格" class="layui-input" disabled="disabled">
            </div>
        </div>
        <input type="hidden" value="${sessionScope.get("userInfo").userId}" name="operator">
        <input type="hidden" name="id">
        <div class="layui-form-item ">
            <label class="layui-form-label">支付类型:</label>
            <div class="layui-input-block">
                <input type="radio" name="payType" value="微信" checked="checked" title="微信" disabled="disabled">
                <input type="radio" name="payType" value="支付宝" title="支付宝" disabled="disabled">
                <input type="radio" name="payType" value="银联" title="银联" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label">备  注:</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入" class="layui-textarea" disabled="disabled"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="margin:10px 140px -15px">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release"
                        lay-submit lay-filter="doSubmit" value="同意入库" name="sub" lay-event="edit">同意入库
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" disabled="disabled">重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->
<!--退货弹出层-->
<div id="exportDiv" style="display: none;padding:20px">
    <form class="layui-form" lay-filter="exportFrm" id="exportFrm">
        <div class="layui-form-item ">
            <label class="layui-form-label">退货数量:</label>
            <div class="layui-input-inline">
                <input type="text" name="number" id="exportnumber" placeholder="请输入退货数量" class="layui-input">
            </div>
            <label class="layui-form-label">退货金额:</label>
            <div class="layui-input-inline">
                <input type="text" name="exportprice" id="exportprice" placeholder="请输入退货金额" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label">退货原因:</label>
            <div class="layui-input-block">
                <textarea name="remark" id="exportRemark" placeholder="请输入退货原因" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item ">
            <label class="layui-form-label">支付类型:</label>
            <div class="layui-input-block">
                <input type="radio" name="paytype" value="微信" checked="checked" title="微信">
                <input type="radio" name="paytype" value="支付宝" title="支付宝">
                <input type="radio" name="paytype" value="银联" title="银联">
            </div>
        </div>
        <input name="number" type="hidden" id="goodsnum">
        <input name="goodsid" type="hidden"/>
        <input name="providerid" type="hidden"/>
        <input name="inportprice" id="inportprice" type="hidden"/>
        <div class="layui-form-item" style="margin:10px 140px -15px">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release"
                        lay-submit lay-filter="doExport">提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh">重置
                </button>
            </div>
        </div>
    </form>
</div>



<script src="../../static/resources/layui/layui.js"></script>
<script type="text/javascript">
    var tableIns;
    var select = "";
    layui.use(['jquery', 'layer', 'form','laydate', 'table'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;

        //渲染商品数据表格
        tableIns = table.render({
            elem: '#inportTable', //渲染的目标对象
            url: 'http://localhost:8080/erp/bus/inport/loadInport', //数据接口
            title: '商品数据表',//数据导出来的标题
            toolbar: "#tableToolBar", //表格的工具条
            defaultToolbar: ['filter', 'exports', 'print'],
            height: 'full-120',
            cellMinWidth: 100, //设置列的最小默认宽度
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: curr - 1
                        }
                    })
                }
            },
            totalRow: false, //开启合并行
            page: true, //是否启用分页
            text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            },
            cols: [ [ //列表数据
                {field: 'status', title: '操作', align: 'center', width: '200',
                    templet: function (d) {
                        if(d.status===0){
                            return '<button lay-event="edit" color=blue>同意入库</button>&nbsp;<button lay-event="del" color=blue>拒绝入库</button>';
                        }
                        else if(d.status===1){
                            return '<font color=green>已入库</font>'
                        }
                        else if(d.status===2){
                            return '<font color=red>拒绝入库</font>'
                        }
                        // return d.status === 1 ? '<font color=blue>有效</font>' : '<font color=red>无效</font>';
                    }},
                {field: 'id', title: 'ID', align: 'center', width: '50'},
                {field: 'goodsName', title: '商品名称', align: 'center', width: '200'},
                {field: 'providerName', title: '供应商名称', align: 'center', width: '150'},
                {field: 'payType', title: '支付类型', align: 'center', width: '100'},
                {field: 'pur_price', title: '进货价格', align: 'center', width: '100'},
                {field: 'goodsNum', title: '数量', align: 'center', width: '100'},
                {field: 'purchaseTime', title: '进货时间', align: 'center', width: '150'},
                {field: 'operator', title: '操作员', align: 'center', width: '80'},
                {field: 'description', title: '备注', align: 'center', width: '200'}
                //{fixed: 'right', title: '操作', toolbar: '#rowToolBar', width: '200', align: 'center'}
            ] ]
        });

        loadProvider(0, $("#providerId1"));
        loadGoods(0, $("#goodsId1"));

        //日期选择器
        laydate.render({
            elem: '#begintime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#endtime'
            ,type: 'datetime'
        });
        function loadProvider(id, select) {
            $.ajaxSettings.async = false;
            //渲染部门下拉框
            var option = "<option value=\"0\">请选择</option>";
            $.get("http://localhost:8080/erp/bus/provider/getProvider", function (result) {
                $.each(result, function (i, e) {
                    if (e.id == id) {
                        option += "<option selected value=" + e.providerId + ">" + e.providerName + "</optionselect>";
                    } else {
                        option += "<option value=" + e.providerId + ">" + e.providerName + "</option>";
                    }
                })
            });
            select.html(option);
            form.render();
            $.ajaxSettings.async = true;
        }

        function loadGoods(id, select) {
            $.ajaxSettings.async = false;
            //渲染部门下拉框
            var option = "<option value=\"0\">请选择</option>";
            $.get("http://localhost:8080/erp/bus/goods/getGoods", function (result) {
                $.each(result, function (i, e) {
                    if (e.id == id) {
                        option += "<option selected value=" + e.goodsId + ">" + e.goodsName + "</optionselect>";
                    } else {
                        option += "<option value=" + e.goodsId + ">" + e.goodsName + "</option>";
                    }
                })
            });
            select.html(option);
            form.render();
            $.ajaxSettings.async = true;
        }
        //顶部模糊查询
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听头部工具栏事件
        table.on("toolbar(inportTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddInport();
                    break;
            }
            ;
        })
        var lineData;
        //监听行工具事件
        table.on('tool(inportTable)', function (obj) {
            lineData = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'del') { //删除
                deleteInport(lineData);
            } else if (layEvent === 'edit') { //编辑
                openUpdateInport(lineData);
            }else if(layEvent === 'notIn'){
                openExport(lineData);
            }
        });
        var url;
        var mainIndex;

        //打开添加页面
        function openAddInport() {
            mainIndex = layer.open({
                type: 1,
                title: '添加商品',
                content: $("#saveOrUpdateDiv"),
                area: ['700px', '560px'],
                success: function (index) {
                    //清空修改页面使用过的表单数据
                    $("#dataFrm")[0].reset();
                    //为弹框中表单提交传递提交地址
                    url = "http://localhost:8080/erp/bus/inport/addInport"
                    loadProvider(0, $("#providerid2"));
                    loadGoods(0,$("#goodsid2"));
                }
            });
        }


        //打开修改页面
        function openUpdateInport(data) {
            var url = "http://localhost:8080/erp/bus/goods/updateGoodsIn";
            layer.confirm('确认同意此货入库吗？', function (index) {
                $.post(url, data, function (result) {
                    if (result.code > 0) {
                        layer.alert(result.msg, {
                            icon: 1
                        });
                        tableIns.reload();
                    } else {
                        layer.alert(result.msg, {
                            icon: 0
                        });
                    }
                });
            });
        }

        // //表单提交数据
        // form.on("submit(doSubmit)", function (result) {
        //     //序列化表单数据
        //     var formData = $("#dataFrm").serialize();
        //     $.post(url, formData, function (result) {
        //         //弹出操作情况数据msg-->封装数据处理请求返回的信息
        //         var code = result.code;
        //         var msg = result.msg;
        //         if (code > 0) {
        //             layer.close(mainIndex);
        //             layer.alert(msg, {
        //                 icon: 1
        //             })
        //             tableIns.reload();
        //         } else {
        //             layer.alert(msg, {
        //                 icon: 0
        //             })
        //         }
        //     })
        // })

        //删除单个商品
        function deleteInport(data) {
            var url = "http://localhost:8080/erp/bus/goods/CannotUpdateGoodsIn";
            layer.confirm('确认拒绝此货入库吗？', function (index) {
                $.post(url, data, function (result) {
                    if (result.code > 0) {
                        layer.alert(result.msg, {
                            icon: 1
                        });
                        tableIns.reload();
                    } else {
                        layer.alert(result.msg, {
                            icon: 0
                        });
                    }
                });
            });
        }
        //打开退货页面
        function openExport(data){
            var url = "http://localhost:8080/erp/bus/goods/CannotUpdateGoodsIn";
            lay.confirm('确认拒绝入库',function (index) {
                $.post(url, data, function (result) {
                    if (result.code > 0) {
                        layer.alert(result.msg, {
                            icon: 1
                        });
                        tableIns.reload();
                    } else {
                        layer.alert(result.msg, {
                            icon: 0
                        });
                    }
                });
            })
        }

    });

</script>

</body>
</html>